package edu.asu.sefcom.crimenet.search;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import edu.asu.sefcom.crimenet.search.data.RelationalDBConnector;
import edu.asu.sefcom.crimenet.search.util.DBUtils;
import edu.asu.sefcom.crimenet.search.util.Util;

public class FilePostExtractor {

	private String parentname;
	
	private String username;
	
	private List<String> fileList;
	
	private RelationalDBConnector dataConnector;
	
	private GraphInfo graphInfo;
	
//	http://entropytc.com/importing-a-mysql-database-into-apache-solr/
//		http://localhost:8080/solr/#/~cores/collection1
//			http://www.arunchinnachamy.com/apache-solr-mysql-data-import/
//				http://stackoverflow.com/questions/1895898/how-to-import-mysql-tables-to-solr
//					http://lucene.apache.org/solr/tutorial.html
//						http://abhirama.wordpress.com/2008/03/20/apache-solr-newbie-guide/
//							https://github.com/evolvingweb/ajax-solr/wiki/Reuters-tutorial%3A-step-1
	
	public FilePostExtractor(String parentname, String username, List<String> files) {
		this.parentname=parentname;
		this.username=username;
		this.fileList=files;
		this.graphInfo=new GraphInfo();
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationConfig.xml");
		dataConnector=(RelationalDBConnector)applicationContext.getBean("relationalDBConnector");
	}

	public static void main(String args[]){
//		String parentname=args[0];
//		String username=args[1];
		String[] parents=new String[]{"bhcrew","CUP","mazafaka","damagelab","ru_hack"};
		for(String parentname:parents){
			List<String> users = Util.returnUsersForparent(parentname);
			for(String username:users){
				List<String> files=null;
				try{
				files=getFiles(parentname,username);
				new FilePostExtractor(parentname,username,files).execute();
				}catch(Exception e){e.printStackTrace();}
		
			}
		}
//		String fileData=Util.readFile("/home/dsm8/Pictures/livejournal_research/damagelab/zd/zdfriends.htm");
//		readPosts(fileData, "ContextualPopup", null);
	}


	private void execute() {
		// TODO Auto-generated method stub
		GraphDatabaseService graphDB=graphInfo.getGraphDB();
		if(fileList==null){
			return;
		}
		for(String filename:fileList){
			String filedata="";
			String filename1="";
			try{
			 filename1=filename.split("[,]")[0];
			 filedata=Util.readFile(filename1);
			}catch(Exception e){
				continue;
			}
			List<String> resultList=new ArrayList<String>();
			try{
			readPosts(filedata, "ContextualPopup", resultList);
//			for(String res:resultList){
//				System.out.println(res);
//			}
			Map<String, String> infoMap=new HashMap<String, String>();
			getUserInfo(graphDB,infoMap);
			String icq=""+infoMap.get("ICQ_UIN"),email=""+infoMap.get("Email"),country=""+infoMap.get("Country");
//			System.out.println(parentname+" | "+username+" | "+icq+" | "+email+" | "+country+" | "+filename1);
			storeData(parentname,username,icq,email,country,filename,resultList);
			}catch(Exception e){e.printStackTrace();}
		}
		graphInfo.shutdown();
	}

	private void storeData(String parentname2, String username2, String icq,
			String email, String country, String filename,
			List<String> resultList) {
		// TODO Auto-generated method stub
		dataConnector.storeContent(parentname2,  username2,  icq,
				 email,  country,  filename, resultList);
	}

	private void getUserInfo(GraphDatabaseService graphDB,
			Map<String, String> infoMap) {
		// TODO Auto-generated method stub
		Node userNode=DBUtils.getNodeByName(graphDB, username, "User");
		String icq="",email="",country="";
		try{
			icq=""+userNode.getProperty("ICQ_UIN");
		}catch(Exception e){}
		try{
			email=""+userNode.getProperty("Email");
		}catch(Exception e){}
		try{
			country=""+userNode.getProperty("Country");
		}catch(Exception e){}
		infoMap.put("Country",country);
		infoMap.put("Email",email);
		infoMap.put("ICQ_UIN",icq);
	}

//	private void storeData(String parentname2, String username2,
//			String filename, List<String> resultList) {
//		// TODO Auto-generated method stub
//		for(String content:resultList){
//			dataConnector.storeContent(parentname2, username2, icq, email, country, filename, resultList);
//		}
//	}


	public static void readPosts(String fileData, String start, List<String> resultList) {
// 		System.out.println(fileData);
//		Integer[] indexes=getIndexes(fileData,start);
//		String search=start+".*\""+start;
		Pattern p = Pattern.compile(
	                start
	                ,Pattern.DOTALL
	            );

	        Matcher matcher = p.matcher(fileData);
	        int prev=0;
	       while(matcher.find()){
	            if(prev>0){
	            	resultList.add(fileData.substring(prev,matcher.end()));
	            }
	            prev=matcher.end();
	        }
//		int lastIndex = 0;
//		int count =0;
//		int prev = 0;
//		while(lastIndex != -1){
//		       lastIndex = fileData.indexOf(start,lastIndex);
//		       
//		       if( lastIndex != -1){
//		             count ++;
//		             resultList.add(lastIndex);
//		             lastIndex+=findStr.length();
//		             if(prev==lastIndex){
//				    	   
//				     }
//		      }
//		       prev=lastIndex;
//		}
//		Integer[] return1=new Integer[resultList.size()];
//		resultList.toArray(return1);
//		System.out.println(count);
//		fileData.indexOf(ch);
//		boolean return1 = false;
//		int beginIndex = 0;
//		int endIndex = 0;
//		int numberOfposts=0;
//		if("".equals(start) || "".equals(end)){
//			throw new IllegalArgumentException("Input is empty");
//		}
//		while (beginIndex != -1) {
//			beginIndex = fileData.indexOf(start, fromInd);
//			endIndex = fileData.indexOf(end, beginIndex);
//			int endlength = end.length();
//			if (beginIndex != -1) {
////				logger.debug("adding**********" + beginIndex + " : " + endIndex);
//				String postData = fileData.substring(beginIndex, endIndex
//						+ endlength);
//				// //pick all the middle parts and place in list
////				logger.debug("added" + postData);
//				resultList.add(postData);
//				numberOfposts++;
//			}
//			fromInd = beginIndex + 1;
//		}
//		logger.error("# of posts read : "+ numberOfposts);
//		return;
	}
	private Integer[] getIndexes(String fileData,String findStr) {
		List<Integer> resultList=new ArrayList<Integer>();
		int lastIndex = 0;
		int count =0;

		while(lastIndex != -1){

		       lastIndex = fileData.indexOf(findStr,lastIndex);

		       if( lastIndex != -1){
		             count ++;
		             resultList.add(lastIndex);
		             lastIndex+=findStr.length();
		      }
		}
		Integer[] return1=new Integer[resultList.size()];
		resultList.toArray(return1);
		System.out.println(count);
		return return1;
	}

	public String getParentname() {
		return parentname;
	}

	public void setParentname(String parentname) {
		this.parentname = parentname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

//	public String getFiles() {
//		return files;
//	}
//
//	public void setFiles(String files) {
//		this.files = files;
//	}
	
	private static List<String> getFiles(String parentuser,
			String username2) {
		// TODO Auto-generated method stub
		String fileData=Util.readFile("/home/dsm8/workspaceCN1/ExtractPosts/config/" + parentuser
							+ "/inputfiles/inputfile"+ username2
							+ ".txt");
		String[] lines = fileData.split("\n");
		List<String> lineL=Arrays.asList(lines);
		return lineL;
	}

}

